import { type CrudConfig, type FormFieldConfig } from '@/components';
import urlConstants from '@/constants/urlConstants';
import type { ProColumns } from '@ant-design/pro-components';
import { Tag } from 'antd';

// 字典数据类型
export interface DictRecord {
  id: string;
  paramIndex: number;
  dictCode: string;
  dictName: string;
  sortOrder: number;
  description: string;
  sysDictItemDTOS?: Array<{
    itemCode: string;
    itemName: string;
    sortOrder: number;
  }>;
  updateUser?: string;
  updateTime?: string;
}

// 数据处理函数
const getRequestData = (postData: DictRecord, type: string): object => {
  console.log('字典数据处理:', postData);
  switch (type) {
    case 'create':
    case 'edit':
      const sysDictItemDTOS = (postData.sysDictItemDTOS || []).map((item: any) => ({
        dictCode: postData.dictCode,
        itemCode: item.itemCode,
        itemName: item.itemName,
        sortOrder: item.sortOrder,
      }));

      return {
        ...postData,
        sysDictItemDTOS,
      };
    default:
      return { ...postData };
  }
};

// 统一的页面配置函数 - 接收 translate 函数作为参数
export const getPageConfig = (translate: (prefix: string, key: string) => string) => {
  const t = (key: string) => translate('systemManage', key);

  return {
    // CRUD配置
    crudConfig: {
      endpoints: {
        list: urlConstants.DICT_MANAGE.LIST,
        create: urlConstants.DICT_MANAGE.CREATE,
        update: urlConstants.DICT_MANAGE.EDIT,
        delete: urlConstants.DICT_MANAGE.DELETE,
        detail: urlConstants.DICT_MANAGE.LIST,
      },
      callbacks: {
        beforeCreate: async (data: any) => getRequestData(data, 'create'),
        beforeUpdate: async (data: any) => getRequestData(data, 'edit'),
      },
    } as CrudConfig,

    // 搜索字段配置
    searchFields: [
      {
        name: 'dictCode',
        label: t('dictCode'),
        type: 'input' as const,
      },
      {
        name: 'dictName',
        label: t('dictName'),
        type: 'input' as const,
      },
    ],

    // 表格列配置
    columns: [
      {
        title: t('serialNumber'),
        dataIndex: 'paramIndex',
        key: 'paramIndex',
        width: 80,
        fixed: 'left',
      },
      {
        title: t('dictCode'),
        dataIndex: 'dictCode',
        key: 'dictCode',
        width: 150,
        copyable: true,
        render: (text: string) => <span style={{ fontWeight: 500, color: '#1890ff' }}>{text}</span>,
      },
      {
        title: t('dictName'),
        dataIndex: 'dictName',
        key: 'dictName',
        width: 200,
        ellipsis: true,
      },
      {
        title: t('sortOrder'),
        dataIndex: 'sortOrder',
        key: 'sortOrder',
        width: 100,
        sorter: true,
      },
      {
        title: t('description'),
        dataIndex: 'description',
        key: 'description',
        width: 200,
        ellipsis: true,
      },
      {
        title: t('dictItemCount'),
        dataIndex: 'sysDictItemDTOS',
        key: 'dictItemCount',
        width: 120,
        render: (text: any[]) => {
          const count = text?.length || 0;
          return (
            <Tag color={count > 0 ? 'green' : 'default'}>
              {count} {t('items')}
            </Tag>
          );
        },
      },
      {
        title: t('updateUser'),
        dataIndex: 'updateUser',
        key: 'updateUser',
        width: 120,
        ellipsis: true,
      },
      {
        title: t('updateTime'),
        dataIndex: 'updateTime',
        key: 'updateTime',
        width: 160,
        valueType: 'dateTime',
        sorter: true,
      },
    ] as ProColumns<DictRecord>[],

    // 表单字段配置
    formFields: [
      {
        name: 'dictCode',
        label: t('dictCode'),
        type: 'input' as const,
        rules: [{ required: true, message: t('dictCodeRequired') }],
      },
      {
        name: 'dictName',
        label: t('dictName'),
        type: 'input' as const,
        rules: [{ required: true, message: t('dictNameRequired') }],
      },
      {
        name: 'sortOrder',
        label: t('sortOrder'),
        type: 'number' as const,
        rules: [{ required: true, message: t('sortOrderRequired') }],
        props: {
          min: 1,
          max: 999,
        },
      },
      {
        name: 'description',
        label: t('description'),
        type: 'textarea' as const,
        span: 24,
      },
      {
        name: 'sysDictItemDTOS',
        label: t('dictItemConfig'),
        type: 'editTable' as const,
        span: 24,
        editTableConfig: {
          columns: [
            {
              title: t('itemCode'),
              dataIndex: 'itemCode',
              type: 'input' as const,
              width: 150,
              required: true,
            },
            {
              title: t('itemName'),
              dataIndex: 'itemName',
              type: 'input' as const,
              width: 200,
              required: true,
            },
            {
              title: t('sortOrder'),
              dataIndex: 'sortOrder',
              type: 'number' as const,
              width: 100,
              required: true,
            },
          ],
          rowKey: 'itemCode',
          maxRows: 50,
          minRows: 1,
          addButtonText: t('addDictItem'),
        },
      },
    ] as FormFieldConfig[],

    // 字典枚举配置
    dictEnum: {
      // 可以在这里添加字典枚举配置
    },
  };
};
